var gulp = require('gulp');
uglify = require("gulp-uglify");
minifyInline = require('gulp-minify-inline');
obfuscator = require('gulp-javascript-obfuscator');
obfuscate = require('gulp-obfuscate');
assetRev = require('gulp-asset-rev');
htmlmin = require('gulp-htmlmin'),
useref = require('gulp-useref');
domSrc = require('gulp-dom-src');
concat = require('gulp-concat');
cssmin = require('gulp-minify-css');
minifyHtml = require("gulp-minify-html");
imagemin = require('gulp-imagemin');
clean = require('gulp-clean');
runSequence = require('run-sequence');

basePath = 'F:\\Downloads\\spon';
filePath = ['bin/**/*.*', 'wuc/**/*.*', 'admin/**/*.*', 'static/js/*.*', 'static/js/ref/*.*', 'static/js/simditor/**/*.*', 'static/js/self/common.js'];
htmlPath = ['*.html', 'my/*.html', 'agent/**/*.html', 'company/**/*.html', 'personal/**/*.html', 'wuc/**/*.html'];

gulp.task('build', function (callback) {
    runSequence('clean', 'app_files', 'app_rev', 'app_js', callback);
});

gulp.task('one', function () {
    domSrc({ file: 'index.html', selector: 'script', attribute: 'src' })
      .pipe(concat('app.full.min.js'))
      .pipe(uglify())
      .pipe(gulp.dest('build/'));
})

gulp.task('two', function () {
    //gulp.src('static/js/self/*.js')
    //.pipe(jshint())
    //.pipe(jshint.reporter());

    //gulp.src(['static/js/self/*.js', '!node_modules/**'])
    //    .pipe(eslint())
    //    .pipe(eslint.format())
    //    .pipe(eslint.failAfterError());
})

function gulpImages() {
    return gulp.src('static/images/*.*')
      .pipe(imagemin())
      .pipe(gulp.dest('build/static/images'));
}

function gulpStyles() {
    return gulp.src('static/css/**/*.css')
    .pipe(assetRev())
    .pipe(cssmin())
    .pipe(gulp.dest('build/static/css'))
}

function gulpFiles() {
    return gulp.src(filePath, { base: basePath })
   .pipe(gulp.dest('build/'));
}

gulp.task('clean', function () {
    return gulp.src('build/', { read: false })
      .pipe(clean());
})

gulp.task('app_files', function () {
    gulpFiles("app");
    gulpImages("app");
    gulpStyles("app");
});

gulp.task('app_rev', function () {
    return gulp.src(htmlPath, { base: basePath })
    .pipe(assetRev({ rootPath: basePath }))
    .pipe(htmlmin({ collapseWhitespace: true, removeComments: true }))
    .pipe(gulp.dest('build/'));
});

gulp.task('app_js', function () {
    return gulp.src(['static/js/self/*.js', '!static/js/self/common.js'])
      .pipe(assetRev())
      .pipe(obfuscator())
      .pipe(uglify())
      .pipe(gulp.dest('build/static/js/self'));
})



